package com.example.fresh_market.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.example.fresh_market.R;
import com.example.fresh_market.entity.Product;
import com.example.fresh_market.utils.HttpUtil;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class ProductAdmAdapter extends ListAdapter<Product, ProductAdmAdapter.ViewHolder> {

    private final OnItemClickListener onItemClickListener;
    private final Context mContext;

    public ProductAdmAdapter(OnItemClickListener onItemClickListener, Context context) {
        super(new DiffCallback());
        this.onItemClickListener = onItemClickListener;
        this.mContext = context;
    }

    protected ProductAdmAdapter(@NonNull DiffUtil.ItemCallback<Product> diffCallback,
                                OnItemClickListener onItemClickListener, Context mContext) {
        super(diffCallback);
        this.onItemClickListener = onItemClickListener;
        this.mContext = mContext;
    }

    public Product getItem(int position) {
        return super.getItem(position);
    }


    @Override
    public void submitList(@Nullable List<Product> list) {
        super.submitList(list);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.prod_list_admin_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Product product = getItem(i);
        viewHolder.bindBean(product);
        if (StringUtils.isNotBlank(product.getImage())) {
            RequestOptions requestOptions = new RequestOptions()
                    .override(120, 160);
            HttpUtil.loadImage(viewHolder.image, product.getImage(), requestOptions);
        } else {
            Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.logo);
            viewHolder.image.setImageDrawable(drawable);
        }
        viewHolder.edit.setOnClickListener(v -> {
            onItemClickListener.onEditClick(v, viewHolder.getAdapterPosition());
        });
        viewHolder.delete.setOnClickListener(v -> {
            onItemClickListener.onDeleteClick(v, viewHolder.getAdapterPosition());
        });
        viewHolder.showSwitch.setOnClickListener(v -> {
            onItemClickListener.onShowClick(viewHolder.itemView, viewHolder.getAdapterPosition());
        });
    }

    public interface OnItemClickListener {
        void onEditClick(View view, int position);

        void onDeleteClick(View view, int position);

        void onShowClick(View view, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView type;
        private final TextView productName;
        private final TextView productInfo;
        private final TextView productPrice;
        private final ImageView image;
        private final Switch showSwitch;
        private final TextView edit;
        private final TextView delete;

        ViewHolder(View item) {
            super(item);
            type = item.findViewById(R.id.productType);
            productName = item.findViewById(R.id.productName);
            productInfo = item.findViewById(R.id.productInfo);
            productPrice = item.findViewById(R.id.productPrice);
            image = item.findViewById(R.id.productImage);
            showSwitch = item.findViewById(R.id.prod_show);
            edit = item.findViewById(R.id.prod_edit);
            delete = item.findViewById(R.id.prod_delete);
        }

        void bindBean(final Product product) {
            type.setText(product.getCateName());
            productName.setText(product.getName());
            productInfo.setText(product.getInfo());
            productPrice.setText(String.format("￥%s", product.getPrice()));
            showSwitch.setChecked(product.getIsShow());
            showSwitch.setText(product.getIsShow() ? "已上架" : "未上架");
        }
    }

    private static class DiffCallback extends DiffUtil.ItemCallback<Product> {
        @Override
        public boolean areItemsTheSame(@NonNull Product oldItem, @NonNull Product newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull Product oldItem, @NonNull Product newItem) {
            return oldItem.equals(newItem);
        }
    }
}
